const mysql2 = require('mysql2');
const Joi = require('@hapi/joi');
const dateFormat = require('dateformat');
const CryptoJS = require("crypto-js");

const appKey = "28127664";
const signSecret = " 4c7d682c552b9dd06ca948f68902f61f";
const password = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(signSecret, signSecret));
// 云函数mysql数据库链接 
let poolObj = mysql2.createConnection({
    host: 'dataproxy-internal.m.mappcloud.com',
    port: 3306,
    user: appKey,
    password: password,
    database: 'test_' + appKey,
    waitForConnections: true,
    connectionLimit: 200,
    pool: {
        idle: 50,  //链接控制时间
    },
    timezone: '+08:00', //东八区
});


/**
 * 插入一条会员信息
 * @param {string} params.mixnick 混淆昵称
 *  */  
exports.createOne = async (params) => {
    const conn = poolObj.promise();
    // sql 数据插入
    let sql = `INSERT INTO shareuser (mixnick,date,gmt_create) VALUES ('${params.mixnick}',now(),now())`;
    let data = await conn.query(sql);
    return {success: true, code: 0, data: data};
}

/**
 * 根据被分享人mixnick查询是否存在
 * @param {string} params.mixnick 混淆昵称
 *  */  
exports.findCountByMixnick = async (params) => {
    const conn = poolObj.promise();
    let sql = `select count(1) as count from shareuser where mixnick = '${params.mixnick}'`;
    let data = await conn.query(sql);
    return {success: true, code: 0, data: data};
}

/**
 * 根据被分享人mixnick查询数据
 * @param {string} params.mixnick 混淆昵称
 *  */  
exports.findByMixnick = async (params) => {
    const conn = poolObj.promise();
    let sql = `select * as count from mf_share_user where mixnick = '${param.mixnick}'`;
    let data = await conn.query(sql);
    return {success: true, code: 0, data: data};
}

